﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Communication;
using System.Net;
using Utilities;
//using System.IdentityModel.Selectors;
//using System.IdentityModel.Tokens;

//using System.Security.Principal;

//using System.ServiceModel;

namespace User
{
    class UserData
    {
        string username;
        public string Username { get { return username; } }
        string password;
        bool isValid = false;
        public bool IsValid { get { return isValid; } }
        private AsyncSender sender;
        private IPEndPoint endPoint;
        public IPEndPoint EndPoint
        {
            get { return endPoint; }
        }
        
        public UserData(string usrnm, string pass, IPAddress ipadd, int portNum)
        {
            this.username = usrnm;
            this.password = pass;
            this.endPoint = new IPEndPoint(ipadd, portNum);
            this.sender = new AsyncSender(endPoint);
            isValid = this.Validate();
        }

        public UserData(string usrnm, string pass, IPEndPoint ipendpoint)
        {
            this.username = usrnm;
            this.password = pass;
            this.endPoint = ipendpoint;
            this.sender = new AsyncSender(endPoint);
            isValid = this.Validate();
        }

        public void Send(string message)
        {
            sender.Send(message);
        }
        // This method validates users. It allows in two users, test1 and test2 
        // with passwords 1tset and 2tset respectively.
        // This code is for illustration purposes only and 
        // must not be used in a production environment because it is not secure.	
        public bool Validate()
        {
            if (null == username || null == password)
            {
                return false;
            }

            if (!(username == "daniel" && password == "1234") && !(username == "test2" && password == "2tset"))
            {
                return false;
            }
            else { return true; }

        }

        public bool Validate(string authentication)
        {
            return true;
        }

        public static bool Validate(string user, string pass)
        {
            if (null == user || null == pass)
            {
                return false;
            }

            if (!(user == "daniel" && pass == "123456") && !(user == "test2" && pass == "2tset"))
            {
                return false;
            }
            else { return true; }
        }

        public void Change(string usrname, string pass)
        {
            this.password = pass;
            this.username = usrname;
            isValid = Validate();
        }
    }
}
